﻿using System;
using UnityEngine;

namespace Yoozoo.Gameplay.Core
{
    public class UpdateDrive:MonoBehaviour
    {
        public bool openOnGUI;
        public bool openOnGizmos;

        public static MonoBehaviour mono { get { return m_instance; } }
        public static event Action updateEventHandler;
        public static event Action lateUpdateEventHandler;
        public static event Action onGizmosEventHandler;
        public static event Action onGUIEventHandler;
        private static UpdateDrive m_instance;

        public static UpdateDrive GetInstance(string objName)
        {
            if (m_instance != null) return null;
            return m_instance=new GameObject(objName).AddComponent<UpdateDrive>();
        }
        private void Awake()
        {
            updateEventHandler = null;
            lateUpdateEventHandler = null;
            onGizmosEventHandler = null;
            onGUIEventHandler = null;
        }
        
        void Start()
        {
            hideFlags = HideFlags.HideInHierarchy;
            DontDestroyOnLoad(gameObject);
        }
        // Update is called once per frame
        void Update()
        {
            if (updateEventHandler != null)
                updateEventHandler();
        }
        void LateUpdate()
        {
            if (lateUpdateEventHandler != null)
                lateUpdateEventHandler();
        }
#if !RELEASE
        void OnGUI()
        {
            if (openOnGUI)
            {
                if (onGUIEventHandler != null)
                    onGUIEventHandler();
            }
        }
#endif

#if UNITY_EDITOR
        void OnGizmos()
        {
            if (openOnGizmos)
            {
                if (onGizmosEventHandler != null)
                    onGizmosEventHandler();
            }
        }
#endif
        void OnDestroy()
        {
            updateEventHandler = null;
            lateUpdateEventHandler = null;
            onGizmosEventHandler = null;
            onGUIEventHandler = null;
            if (m_instance != null)
            {
                m_instance = null;
            }
        }
    }
}